package com.yzion.widgetlib.refreshrecyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.internal.InternalAbstract;
import com.yzion.widgetlib.R;

/**
 * ZJJ
 **/
public class DefaultRefreshFooter extends InternalAbstract implements RefreshFooter {

    private TextView mTvHint;
    private ProgressBar mProgress;

    public static DefaultRefreshFooter newInstance(Context context) {
        View wrapper = LayoutInflater.from(context).inflate(R.layout.by_footer_refresh_layout, null);
        return new DefaultRefreshFooter(wrapper);
    }

    protected DefaultRefreshFooter(@NonNull View wrapper) {
        super(wrapper);
        mTvHint = wrapper.findViewById(R.id.pull_to_refresh_text);
        mProgress = wrapper.findViewById(R.id.pull_to_refresh_progress);
    }


    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        super.onStateChanged(refreshLayout, oldState, newState);
//        LogUtils.d("onStateChanged:" + GsonUtils.toJson(newState));
        if (newState == RefreshState.Loading) {
            mTvHint.setText(getContext().getString(R.string.loading));
            mProgress.setVisibility(View.VISIBLE);
        } else if (newState == RefreshState.LoadFinish) {
            mTvHint.setText(getContext().getString(R.string.load_finish));
            mProgress.setVisibility(View.GONE);
        } else if (newState == RefreshState.PullUpToLoad) {
            mTvHint.setText(getContext().getString(R.string.load_more));
            mProgress.setVisibility(View.GONE);
        }
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
//        LogUtils.d("onFinish:" + success);
        return super.onFinish(refreshLayout, success);
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        super.onStartAnimator(refreshLayout, height, maxDragHeight);
//        LogUtils.d("onStartAnimator");
    }

    @Override
    public boolean setNoMoreData(boolean noMoreData) {
        if (noMoreData) {
            mProgress.setVisibility(View.GONE);
            mTvHint.setText(getContext().getString(R.string.no_more_data));
        }
        return noMoreData;
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        super.onReleased(refreshLayout, height, maxDragHeight);
//        LogUtils.d("onReleased");

    }
}
